/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandAddJoint.js<br>
 * 日期：2016年9月20日<br>
 * 作者: 王志远<br>
 * 功能：添加命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandAddJoint<br>
 * 类型功能：添加元件命令<br>
 */
Rainier.UI.V2.CommandAddJoint = draw2d.command.Command.extend({
	
	NAME : "Rainier.UI.V2.CommandAddJoint",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(){
		this._super();
		this.source 			= null;
		this.targetConnection 	= null;
		this.jointLocation		= null;
		this.vertices			= null;
		this.segments			= null;
		
		this.createdJoint		= null;
		this.createdConnection	= null;
		
		this.undoInfo			= null;
		this.context			= null;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.setSourcePort<br>
	 * 函数功能：设置源端口<br>
	 * 参数输入：source:draw2d.Port<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setSourcePort : function(source){
		this.source = source;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.setTargetConnection<br>
	 * 函数功能：设置需要创建节点的目标连接<br>
	 * 参数输入：targetConn:draw2d.Connection<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setTargetConnection : function(targetConn){
		this.targetConnection = targetConn;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.setJointLocation<br>
	 * 函数功能：设置节点位置<br>
	 * 参数输入：jointLoc:draw2d.geo.Point<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setJointLocation : function(jointLoc){
		this.jointLocation = $.extend({},jointLoc,true);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.setVertices<br>
	 * 函数功能：设置新连接的顶点列表<br>
	 * 参数输入：verticesc:draw2d.util.ArrayList<draw2d.geo.Point><br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setVertices : function(vertices){
		this.vertices = vertices.clone();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.setSegments<br>
	 * 函数功能：设置新连接的顶点线段<br>
	 * 参数输入：segmentList:draw2d.util.ArrayList<><br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setSegments : function(segmentList){
		this.segments = segmentList.clone();
	},
	
    /**
	 * 函数名称：Rainier.UI.V2.CommandMove.canExecute<br>
	 * 函数功能：能否执行<br>
	 * 函数返回：Boolean<br>
	 * 异常信息：无<br>
	 */
    canExecute:function(){
    	if( this.source && 
    		this.targetConnection && 
    		this.jointLocation && 
    		this.vertices && 
    		this.setSegments){
    		
    		return true;
    	}
    	return false;
    },
    
    
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.execute<br>
	 * 函数功能：执行命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute:function(){
    	console.log("Rainier.UI.V2.CommandAddJoint.execute");
    	var canvas 		   = this.source.getCanvas();
    	this.context   	   = canvas.context;
    	var sourcePortPart = this.context.findEditPartByFigure(this.source);
    	var targetConnPart = this.context.findEditPartByFigure(this.targetConnection);
    	var sourcePortModel= null;
    	var targetConnModel= null;
    	
    	if(sourcePortPart && targetConnPart){
    		sourcePortModel = sourcePortPart.getModel();
    		targetConnModel = targetConnPart.getModel();
    	}
    	
    	if(!(sourcePortModel && targetConnModel)){
    		return;
    	}
    	
    	var jointModel = new Rainier.UI.V2.JointModel();
    	var jointWdith = jointModel.property.uiProperty.width;
    	var jointHeight= jointModel.property.uiProperty.height;
    	var jointLocX  = this.jointLocation.x-jointWdith/2;
    	var jointLocY  = this.jointLocation.y-jointHeight/2;
    	var jointOffset= this.jointLocation.offset;
    	
    	jointModel.property.uiProperty.x = jointLocX;
    	jointModel.property.uiProperty.y = jointLocY;
    	jointModel.property.uiProperty.offset = jointOffset;
    	
    	targetConnModel.addChildModel(jointModel,false);

    	this.createdJoint = jointModel;
    	
   		var property = {};
		property.uiProperty={
//			'outlineStroke'	:1,
//			'outlineColor'	:'#303030',
			'stroke'		:1,
			'radius'		:1,
			'color'			:'#00A8F0'
		};
		
		property.modelProperty={
				name	  : 'connection1',
				router	  : 'Rainier.UI.V2.InteractiveManhattanRouter',
				segments  : [],
				source_id : sourcePortModel.getUuid(),
				target_id : this.createdJoint.getHybridPorts().get(0).getUuid(),
		};
		
		this.segments.each(function(i,seg){
			var _seg = {
					start_x:seg.start.x,
					start_y:seg.start.y,
					end_x:seg.end.x,
					end_y:seg.end.y,
					type:seg.type
			};
			property.modelProperty.segments.push(_seg);
		});

		var connParentPart  	= sourcePortPart.getParent().getParent();
		var connParentModel		= connParentPart.getModel();
		if(connParentModel instanceof Rainier.UI.V2.ConnectionModel){
			connParentModel = connParentModel.getParent();
		}
		this.createdConnection 	= connParentModel.connectByProperty(this.context,property,false);
		connParentPart.refreshChildren();
		
		this.context.circuitparser.parse();
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.undo<br>
	 * 函数功能：取消操作命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo:function(){
    	var rootEditPart = this.context.getRootEditPart();
    	var rootModel 	 = this.context.getRootModel();
    	
    	this.undoInfo    = rootModel.removeConnection(this.createdConnection);
    	rootEditPart.refreshChildren();
		this.context.circuitparser.parse();
    },

    /**
	 * 函数名称：Rainier.UI.V2.CommandAddJoint.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo:function(){
    	if(this.undoInfo){
    		var _this = this;
    		var currentModel 	 = this.undoInfo.model;
    		var connInfoStack	 = this.undoInfo.deleteConnInfoStack.clone();

    		connInfoStack.each(function(index,connModelInfo){
    			
    			var connType	= connModelInfo.deleteConnType;
    			var connObj 	= connModelInfo.connModel;
    			var sourceModel = connModelInfo.sourceModel;
    			var targetModel = connModelInfo.targetModel;
    			
    			//第一步：把当前连线添加进来
    			var conn = connObj.connection;
    			
    			//第二步：恢复连线和sourcePort和targetPort的关联关系。
    			var sourcePort = connObj.sourcePort;
    			var targetPort = connObj.targetPort;
    			conn.attachSourcePort(sourcePort);
    			conn.attachTargetPort(targetPort);
    			sourcePort.attachOutgoing(conn);
    			targetPort.attachIncoming(conn);
    			
    			var connParent = conn.getParent();
    			connParent.addConnection(conn);
    			
    			//第三步：恢复当前连线上长的jointModel
    			if(sourceModel){
    				var jointModel 		 = sourceModel.jointModel;
    				var hybridPort		 = sourceModel.hybirdPort;
    				
    				jointModel.addPort(hybridPort);
    				
    				var jointModelParent = jointModel.getParent();
    				jointModelParent.addChildModel(jointModel);
    			}
    			
    			if(targetModel){
    				var jointModel 		 = targetModel.jointModel;
    				var hybridPort		 = targetModel.hybirdPort;
    				
    				jointModel.addPort(hybridPort);
    				
    				var jointModelParent = jointModel.getParent();
    				jointModelParent.addChildModel(jointModel);
    			}
//    			_this.context.rootModel.setConnectionPortValue(sourcePort,targetPort);
			})			
			var rootEditPart = this.context.getRootEditPart();
			rootEditPart.refreshChildren();
			
			this.context.circuitparser.parse();
			this.context.rootModel.getConnections().each(function(index,conn){
	    		conn.changeNodeLabel(true,false);
	    	})
    	}
    }

});

